<?php
namespace app\api\controller\orderreceipt;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\orderreceipt\ReceiptList;
use app\common\model\buss\Buss;
use app\common\model\power\Rolepowerlnk;
use app\api\controller\Base;
use app\common\model\power\Rolepower;
class Manaorderreceipt extends Base
{
	/**
	 * 产生jwt加密串，参考session
	 */
    public function initialize(){
		parent::initialize();
		self::is_login();
		Rolepower::powerguard(204005);
	}

	/**
	 * 查看订单信息
	 * @return [type] [description]
	 */
	public function getinfo(){
		$id = input('id');
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$data = ReceiptList::getinfo($id,'mana');
		if(!$data['code']){
			return myerr($data['msg']);
		}
		$data = $data['msg'];
		$data['power'] = Rolepowerlnk::getpowerlnk('orderhawa');
		$data['CELLPHONE'] = CELLPHONE;
		return myok($data);
	}
	
	/**
	 * 修改/完善企业信息 
	 * 产废人信息也再次完善
	 * @return [type] [description]
	 */
	public function list(){
		$data = input('get.');
		$data['Type'] = 'ware';
		$data = init_where($data);
		$list = ReceiptList::Managetlist($data);
		foreach ($list['list'] as $key => $value) {
			if(empty($value['bussinfo']['UserName'])){
				if(!empty($value['tranuserinfo']['Name'])){
					$list['list'][$key]['Name'] = $value['tranuserinfo']['Name'];
				}else{
					if(!empty($value['bussuserinfo']['Name'])){
						$list['list'][$key]['Name'] = $value['bussuserinfo']['Name'];
					}else{
						$list['list'][$key]['Name'] = '暂未完善名称';
					}
				}
			}else{
				$list['list'][$key]['Name'] = $value['bussinfo']['UserName'];
			}
			
			if($value['TotalCost']>=0){
				$list['list'][$key]['TotalCost'] = '收费'.$value['TotalCost'];
			}else{
				$list['list'][$key]['TotalCost'] = '补贴'.(-1*$value['TotalCost']);
			}
		}
		$list['tr']['title']  = ['收集员','入库时间','订单状态','查看','确认时间','合计金额','结算时间'];
		$list['tr']['key'] = ['Name','WarehouseTime','State','ReceiptListId','BussinessTime','TotalCost','SettlementTime'];
		return myok($list);
	}

	/**
	 * 地图查看订单（分配使用）
	 * @return [type] [description]
	 */
	public function maplist(){
		$data = input('post.');
		$data['state'] = empty($data['state'])?0:$data['state'];
		$list = ReceiptList::Managetlist($data,$data['state']);
		foreach ($list['list'] as $key => $value) {
			if(!empty($value['companyinfo'])){
				$list['list'][$key] = array_merge($list['list'][$key] , $value['companyinfo']);
			}
			if(empty($list['list'][$key]['Name'])){
				$list['list'][$key]['Name'] = '暂未完善名称';
			}
		}
		$list['tr']['title']  = ['产废单位','下单时间','订单状态','查看'];
		$list['tr']['key'] = ['Name','UpTime','State','ReceiptListId'];
		$list['buss'] = Buss::getoklist(['UserName','Address','UserTel','UserId']);
		return myok($list);
	}

	/**
	 * 订单分配
	 * @return [type] [description]
	 */
	public function dist(){
		$data = init_post(input('post.'));
		$sb = ReceiptList::test_state($data['ids'],ReceiptList::STATEINT);
		if(!$sb){
			return myerr('数据异常，请重新尝试');
		}
		$data = ReceiptList::dist($data);
		return myok($data['msg']);
	}
	/**
	 * 变更收集员
	 * @return [type] [description]
	 */
	public function changebuss(){
		$data = input('post.');
		$sb = ReceiptList::changedist($data);
		return myok($sb['msg']);
	}
	/**
	 * 变更浮差
	 * @return [type] [description]
	 */
	public function changeprice(){
		Rolepower::powerguard(205003);
		$id = input('id');
		$data = ReceiptList::liststate($id);
		if(empty($data)){
			return myerr('没有找到需要的订单');
		}
		$data = $data['msg'];
		$rdata = [];
		$rdata['id'] = $id;
		if(!empty(input('FloatingCost'))){
			$rdata['FloatingCost']  = input('FloatingCost');
		}
		if(!empty(input('FloatingNote'))){
			$rdata['FloatingNote']  = input('FloatingNote');
		}
		if(!empty($data['FloatingCost'])){
			$data['TotalCost'] = $data['TotalCost'] - $data['FloatingCost'];
		}
		$rdata['TotalCost'] = $data['TotalCost'] + $rdata['FloatingCost'];
		ReceiptList::user_reinfo($rdata);
		//检查结算账单是否一致
		if(!empty($data['SettlementTime'])){
			//会计已经结算，但是修改了价格
			return myok('修改成功，已通知会计价格变动');
		}else{
			return myok('修改成功');
		}
	}
	/**
	 * 图片上传
	 * @return [type] [description]
	 */
	public function upimg(){
		$type = empty(input('type'))?'other':input('type');
		if(!$type=='company'||!$type=='user'||!$type=='order'){
			$type = 'other';
		}
		try {
			$file = request()->file('img');
		} catch (\Exception $e) {
			return json_encode(['status'=>'error','data'=>$e->getMessage()]);
		}
        // 移动到框架应用根目录/public/uploads/ 目录下
        if(!is_null($file)){
           	try{
           		$save_path = Env::get('root_path').'public\\static' .'\\upimg\\hawa\\'.$type.'\\';
	            $info = $file->validate(["size"=>50000000,'ext'=>'jpg,png,gif'])->rule("date")->move($save_path);
	            //echo $save_path. DS . date("Ymd") . DS .  $info->getFilename();
	            $image = \think\Image::open($save_path . '\\' . $info->getSaveName());  
	            $image->thumb(2000, 2000)->save($save_path .  '\\' . $info->getSaveName());  
	            if($info){//getSaveName();
	                $p=$info->getFilename();
	                return json_encode(['status'=>'ok','data'=>SERVERURL.'static/upimg/hawa/'.$type.'/'.date('Ymd',time()).'/'.$p]);
	            }else{
	              return json_encode(['status'=>'error','data'=>$info->getError()]);
	            }
           	} catch (\Exception $e) {
				return json_encode(['status'=>'error','data'=>$e->getMessage()]);
			}	
            
         }else{
              return json_encode(['status'=>'error','data'=>'没有找到需要上传的图片']);
         }
	}


	/**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}
}
